package com.hanserwei.han_picture.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanserwei.han_picture.annotation.AuthCheck;
import com.hanserwei.han_picture.constant.UserConstant;
import com.hanserwei.han_picture.domain.common.ApiResponse;
import com.hanserwei.han_picture.domain.common.DeleteRequest;
import com.hanserwei.han_picture.domain.entity.po.Space;
import com.hanserwei.han_picture.domain.entity.po.SpaceLevel;
import com.hanserwei.han_picture.domain.entity.po.User;
import com.hanserwei.han_picture.domain.entity.vo.SpaceVO;
import com.hanserwei.han_picture.domain.request.space.SpaceAddRequest;
import com.hanserwei.han_picture.domain.request.space.SpaceEditRequest;
import com.hanserwei.han_picture.domain.request.space.SpaceQueryRequest;
import com.hanserwei.han_picture.domain.request.space.SpaceUpdateRequest;
import com.hanserwei.han_picture.enums.ResponseCodeEnum;
import com.hanserwei.han_picture.enums.SpaceLevelEnum;
import com.hanserwei.han_picture.manager.auth.SpaceUserAuthManager;
import com.hanserwei.han_picture.service.SpaceService;
import com.hanserwei.han_picture.service.seviceImpl.UserServiceImpl;
import com.hanserwei.han_picture.utils.ThrowUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;


/**
 * 空间(space)表控制层
 *
 * @author hanserwei
 */
@RestController
@RequestMapping("/space")
@RequiredArgsConstructor
@Tag(name = "SpaceController")
public class SpaceController {

    private final SpaceService spaceService;
    private final UserServiceImpl userServiceImpl;
    private final SpaceUserAuthManager spaceUserAuthManager;


    @GetMapping("/get")
    @Operation(summary = "根据id获取空间信息(管理员)")
    @AuthCheck(requiredRole = UserConstant.ADMIN_ROLE)
    public ApiResponse<Space> getSpaceById(@RequestParam("id") Long id) {
        ThrowUtils.throwIf(id == null || id <= 0, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        Space space = spaceService.getById(id);
        ThrowUtils.throwIf(space == null, ResponseCodeEnum.RESOURCE_NOT_FOUND, "空间不存在！");
        return ApiResponse.success(space);
    }

    @GetMapping("/get/vo")
    @Operation(summary = "根据id获取空间信息(用户)")
    public ApiResponse<SpaceVO> getSpaceVOById(@RequestParam("id") Long id, HttpServletRequest request) {
        ThrowUtils.throwIf(id == null || id <= 0, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        Space space = spaceService.getById(id);
        ThrowUtils.throwIf(space == null, ResponseCodeEnum.RESOURCE_NOT_FOUND, "空间不存在！");
        User loginUser = userServiceImpl.getLoginUser(request);
        List<String> permissionList = spaceUserAuthManager.getPermissionList(space, loginUser);
        SpaceVO spaceVO = spaceService.getSpaceVO(space);
        spaceVO.setPermissionList(permissionList);
        return ApiResponse.success(spaceVO);
    }

    @PostMapping("/list/page")
    @Operation(summary = "分页获取空间列表(管理员)")
    @AuthCheck(requiredRole = UserConstant.ADMIN_ROLE)
    public ApiResponse<Page<Space>> listSpaceByPage(@RequestBody SpaceQueryRequest queryRequest) {
        ThrowUtils.throwIf(queryRequest == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        Page<Space> spacePage = spaceService.listSpaceByPage(queryRequest);
        return ApiResponse.success(spacePage);
    }

    @PostMapping("/list/page/vo")
    @Operation(summary = "分页获取空间列表(用户)")
    public ApiResponse<Page<SpaceVO>> listSpaceVOByPage(@RequestBody SpaceQueryRequest queryRequest) {
        ThrowUtils.throwIf(queryRequest == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        ThrowUtils.throwIf(queryRequest.getPageSize() > 20, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        Page<Space> spacePage = spaceService.listSpaceByPage(queryRequest);
        Page<SpaceVO> spaceVOPage = spaceService.getSpaceVOPage(spacePage);
        return ApiResponse.success(spaceVOPage);
    }

    @GetMapping("/list/level")
    @Operation(summary = "获取空间等级列表，便于前端展示")
    public ApiResponse<List<SpaceLevel>> listSpaceLevel() {
        SpaceLevelEnum[] spaceLevelEnums = SpaceLevelEnum.values();
        Stream<SpaceLevelEnum> levelEnumStream = Arrays.stream(spaceLevelEnums);
        List<SpaceLevel> spaceLevelList = levelEnumStream.map(spaceLevelEnum -> new SpaceLevel(spaceLevelEnum.getValue(), spaceLevelEnum.getText(), spaceLevelEnum.getMaxSize(), spaceLevelEnum.getMaxCount())).toList();
        return ApiResponse.success(spaceLevelList);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除空间")
    public ApiResponse<Boolean> deleteSpace(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(deleteRequest == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        ThrowUtils.throwIf(deleteRequest.getId() <= 0, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        User loginUser = userServiceImpl.getLoginUser(request);
        boolean result = spaceService.deleteSpace(deleteRequest.getId(), loginUser);
        return ApiResponse.success("删除成功！", result);
    }

    @PostMapping("/update")
    @Operation(summary = "更新空间(管理员)")
    @AuthCheck(requiredRole = UserConstant.ADMIN_ROLE)
    public ApiResponse<Boolean> updateSpace(@RequestBody SpaceUpdateRequest request) {
        ThrowUtils.throwIf(request == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        ThrowUtils.throwIf(request.getId() <= 0, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        boolean result = spaceService.updateSpace(request);
        return ApiResponse.success("更新成功！", result);
    }

    @PostMapping("/edit")
    @Operation(summary = "编辑空间(用户)")
    public ApiResponse<Boolean> editSpace(@RequestBody SpaceEditRequest editRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(editRequest == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        ThrowUtils.throwIf(editRequest.getId() <= 0, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        User loginUser = userServiceImpl.getLoginUser(request);
        boolean result = spaceService.editSpace(editRequest, loginUser);
        return ApiResponse.success("编辑成功！", result);
    }

    @PostMapping("/add")
    @Operation(summary = "创建空间(用户)")
    public ApiResponse<Long> addSpace(@RequestBody SpaceAddRequest addRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(addRequest == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        User loginUser = userServiceImpl.getLoginUser(request);
        Long spaceId = spaceService.addSpace(addRequest, loginUser);
        return ApiResponse.success("创建成功！", spaceId);
    }

}
